#!/bin/bash
#while true;do netstat -lntup | grep ":80 " ;done

logfile=`date "+%Y-%m-%d_%H:%M"`
dep() {
    cd client && cnpm install && cnpm run build
    cd ..
    cd other/jsModule && cnpm install
    cd ../..
    cd server && cnpm install
}
deldep() {
    rm ./server/dist -rf
    find . -name "node_modules" | xargs rm -rf
}
start() {
    cd ./server && nohup npm start > ../other/log/$logfile.log 0>&1 2>&1 & 
}
stop() {
    pid=`netstat -lntup | grep ":::80 .*LISTEN" | awk '{print $NF}' | grep -o "[0-9]\{1,\}"`
    [ -z $pid ] && {
        return
    }
    kill -9 $pid
}
restart() {
    stop
    start
}
clear() {
	stop
    rm ./other/log/*
}


case $1 in
    start)
    start
    exit 0
    ;;
    stop)
    stop
    exit 0
    ;;
    restart)
    restart
    exit 0
    ;;
    clear)
    clear
    exit 0
    ;;
    dep)
    dep
    exit 0
    ;;
    deldep)
    deldep
    exit 0
    ;;
    *)
    echo "Usage : $0 {start|stop|restart|clear|dep|deldep}"
    ;;
esac
